{* Smarty *}
(function($) {
    // Defined menu template: lv1, lv2, lv3
    var menu_lv1_template = [
        '<h2>',
            '<a href="#" class="btnClose">&nbsp;</a>',
            '<a href="#"><span></span></a>',
        '</h2>'
    ].join('');

    var menu_lv2_template = [
        '<li>',
            '<a href="#"><span></span></a>',
        '</li>'
    ].join('');

    var menu_lv3_template = [
        '<li>',
            '<h3><a href="#"><span></span></a></h3>',
        '</li>'
    ].join('');

    function MenuNode(segment, name, url, sub_menu) {
        this.segment = segment;
        this.name = name;
        this.url = url;
        this.sub_menu = sub_menu;
    }

{literal}{$tpl_level_1_begin = TRUE}{/literal}

    var oa_menu = [
{foreach $oa_menu as $level_1}
    {literal}{if $SESSION->is_master() || $SESSION->get_role()|array_intersect:[{/literal}{$level_1_roles.{$level_1.segment}|implode:', '}{literal}]|count > 0}{/literal}
        {literal}{if ! $tpl_level_1_begin}{','}{/if}{$tpl_level_1_begin = FALSE}{/literal}
        {strip}
        new MenuNode(
            '{$level_1.segment}',
            '{$level_1.name}',
            '{$level_1.url}',
            [
        {/strip}

    {literal}{$tpl_level_2_begin = TRUE}{/literal}
    {foreach $level_1.sub_menu as $level_2}
        {literal}{if $SESSION->is_master() || $SESSION->get_role()|array_intersect:[{/literal}{$level_2_roles.{$level_1.segment}.{$level_2.segment}|implode:', '}{literal}]|count > 0}{/literal}
            {literal}{if ! $tpl_level_2_begin}{','}{/if}{$tpl_level_2_begin = FALSE}{/literal}
            {strip}
                new MenuNode(
                    '{$level_2.segment}',
                    '{$level_2.name}',
                    '{$level_2.url}',
                    [
            {/strip}

        {literal}{$tpl_level_3_begin = TRUE}{/literal}
        {foreach $level_2.sub_menu as $level_3}
            {literal}{if $SESSION->is_master() || $SESSION->get_role()|array_intersect:[{/literal}{$level_3_roles.{$level_1.segment}.{$level_2.segment}.{$level_3.segment}|implode:', '}{literal}]|count > 0}{/literal}
                {literal}{if ! $tpl_level_3_begin}{','}{/if}{$tpl_level_3_begin = FALSE}{/literal}
                {strip}
                        new MenuNode(
                            '{$level_3.segment}',
                            '{$level_3.name}',
                            '{$level_3.url}',
                            []
                        ) 
                {/strip}
            {literal}{/if}{/literal}
        {/foreach}
                    ]
                )
        {literal}{/if}{/literal}
    {/foreach}
            ]
        )
    {literal}{/if}{/literal}
{/foreach}
    ];

    function create_menu(menu_top_id, tabs_top_id, menu_location_id)
    {
        // parse url
        var menu_index = get_menu_index_from_url();
        var index_top = menu_index[0];
        var index_sub = menu_index[1];
        var index_tab = menu_index[2];

        // Show left menu
        var menu_top = $('#' + menu_top_id);
        $.each(oa_menu, function(index_lv1, node_lv1) {
            var menu_lv1 = create_menu_node(menu_lv1_template, node_lv1, menu_top);

            if (node_lv1.sub_menu.length > 0)
            {
                var ul = $("<ul>");
                ul.insertAfter(menu_lv1);

                $.each(node_lv1.sub_menu, function(index_lv2, node_lv2)
                {
                    // Set node_lv2 url
                    if (node_lv2.sub_menu.length > 0)
                    {
                        var first_node_lv3 = node_lv2.sub_menu[0];
                        node_lv2.url = first_node_lv3.url;
                    }

                    sub_menu_obj = create_menu_node(menu_lv2_template, node_lv2, ul);

                    // Set active class
                    if (index_lv1 == index_top && index_lv2 == index_sub)
                    {
                        sub_menu_obj.filter('li').addClass('on'); 
                    }
                });
            }
        });

        // Show tabs menu
        var tabs_top = $('#' + tabs_top_id);

        if (typeof(index_tab) != 'undefined')
        {
            var menu_tabs = oa_menu[index_top].sub_menu[index_sub].sub_menu;
            $.each(menu_tabs, function(index_lv3, node_lv3) {
                var new_menu = create_menu_node(menu_lv3_template, node_lv3, tabs_top);
                if (index_lv3 == index_tab)
                {
                    new_menu.addClass('on').find('a').removeAttr('href');
                }
            });

            tabs_top.parent().show();
        }


        // Show menu location
        var menu_location = $('#' + menu_location_id);
        if (menu_location.size() == 1 && typeof(index_sub) != 'undefined')
        {
            menu_location.html(oa_menu[index_top].name);
            var sep = ' &raquo; ';
            $('<span>').html(sep + oa_menu[index_top].sub_menu[index_sub].name).appendTo(menu_location);
        }
    }


    // Create new menu in term of template, and append it to menu_parent
    function create_menu_node(menu_template, menu_node, menu_parent)
    {
        return $(menu_template).find('span').html(menu_node.name).end()
            .find('a').attr('href', menu_node.url ? menu_node.url : '#').end()
            .appendTo(menu_parent);
    }


    // Get menu index from url, return [index_top, index_sub, index_tab].
    function get_menu_index_from_url()
    {
        var url_obj = parseUri(window.location);
        var part_list = $.grep(url_obj.path.split('/'), function(n, i) {
            return n != '' && n != 'index.php';
        }).slice(0, 3);

        var menu_index = [];
        var curr_menu = oa_menu;
        $.each(part_list, function(i, segment) {
            if (curr_menu.length == 0)
            {
                return false;
            }

            var index = -1;
            $.each(curr_menu, function(j, node) {
                if (segment == node.segment)
                {
                    if (segment == node.segment)
                    {
                        index = j;
                        return false;
                    }
                }
            });

            if (index == -1)
            {
                return false;
            }

            menu_index.push(index);
            curr_menu = curr_menu[index].sub_menu;
        });

        return menu_index;
    }

    window.create_menu = create_menu;

})(jQuery);
